package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.atguigu.jxc.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("/supplier")
@RestController
public class SupplierController {

    @Autowired
    private SupplierService supplierService;
    /**
     * 分页查询供应商
     * @param page 当前页数
     * @param rows 每页显示的记录数
     * @param supplierName 供应商名字
     * @return map
     */
    @PostMapping("list")
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = supplierService.list(page, rows, supplierName);
        return map;
    }

    /**
     * 供应商添加或修改
     * @param supplierId
     * @param supplier
     * @return ServiceVO
     */
    @PostMapping("save")
    public ServiceVO save(Integer supplierId, Supplier supplier) {

        return supplierService.save(supplierId, supplier);
    }

    /**
     * 批量删除供应商
     * @param ids 1,4,6
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(String ids) {
        return supplierService.delete(ids);
    }


    /**
     * 供应商下拉列表查询, 加供应商名称模糊查询
     * @param q
     * @return
     */
    @PostMapping("getComboboxList")
    public List<Supplier> getComboboxList(String q) {
        QueryWrapper<Supplier> supplierQueryWrapper = null;
        if (!StringUtil.isEmpty(q)) {
            supplierQueryWrapper.like("supplier_name", q);
        }
        List<Supplier> list = supplierService.list(supplierQueryWrapper);
        return list;
    }
}
